from logging import exception
from selenium.webdriver.support import expected_conditions
from time import sleep, strftime,localtime,time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ForumDriver
from selenium.common.exceptions import NoSuchFrameException

scrpath = "C:\\ForumTest\\Screenshot"

class ForumUser:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = ForumDriver.driver
        url = "http://127.0.0.1:58080/index.html"
        self.driver.get(url)

    def UserPost(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_profile").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div")
        self.driver.find_element(By.CSS_SELECTOR,"#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR, "#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)")
        self.driver.find_element(By.CSS_SELECTOR, "#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)")
        self.driver.find_element(By.CSS_SELECTOR,"#profile_avatar")
        self.driver.find_element(By.CSS_SELECTOR,"#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div")
        file_name = strftime("%Y-%m-%d %H-%M-%S UserPost", localtime()) + ".png"
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)

    def UserCenter(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_settings").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#settings_nickname")
        self.driver.find_element(By.CSS_SELECTOR,"#setting_input_nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#setting_input_nickname").click()
        self.driver.find_element(By.CSS_SELECTOR, "#setting_input_nickname").send_keys("zhang")
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#setting_submit_nickname").click()
        self.driver.find_element(By.CSS_SELECTOR,"#setting_input_email").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#setting_input_email").click()
        self.driver.find_element(By.CSS_SELECTOR, "#setting_input_email").send_keys("12345678910@email.com")
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#setting_submit_email").click()
        self.driver.find_element(By.CSS_SELECTOR,"#setting_input_phoneNum").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#setting_input_phoneNum").click()
        self.driver.find_element(By.CSS_SELECTOR,"#setting_input_phoneNum").send_keys("1234567890")
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#setting_submit_phoneNum").click()
        self.driver.find_element(By.CSS_SELECTOR,"#settings_textarea_remark").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#settings_textarea_remark").send_keys("你好，自动化测试！")
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#settings_submit_remark").click()
        file_name = strftime("%Y-%m-%d %H-%M-%S UserCenter", localtime()) + ".png"
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)

    def Userpassword(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_settings").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#settings_input_oldPassword").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_newPassword").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#settings_input_passwordRepeat").send_keys("1234")
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#settings_submit_password").click()